const app = getApp();
import util from "../../../../utils/util";
import tools from "../../../../utils/tools.js";
var list = [];
var type = "";
var type_id = "";
var valueid = "";
var apitype = "";
var value = "";
Page({
    data: {
        folderList: [],
        typeid: "",
    },
    onLoad: function(options) {
        type = options.type;
        type_id = options.type_id;
        valueid = options.valueid;
        apitype = options.apitype;
        value = options.value;
        util.getUrl(app.globalData.url).then(res => {
            util.request("user.php", { action: "collect_type" }, "POST").then(res => {
                if (res.data.message == "success") {
                    res.data.data.type.forEach(item => this.data.folderList.push({ collect: item }))
                    this.setData({
                        folderList: this.data.folderList,
                    });
                    list = res.data.data.name;
                }

            });
        });
    },

    click(e) {
        let index = e.currentTarget.dataset.index;
        let id = e.currentTarget.dataset.id;
        let data = this.data.folderList[index];
        if (id) {
            this.setData({
                typeid: data.id,
                typename: data.collect,
            });
        } else {
            let child = this.getChild(data.collect);
            if (data.child) {
                this.data.folderList.splice(index + 1, data.childlength);
                this.data.typeid = 0;
                this.data.typename = "";
                this.data.folderList[index].child = false;
            } else {
                this.data.folderList.splice(index + 1, 0, ...child);
                this.data.folderList[index].child = true;
                this.data.folderList[index].childlength = child.length;
                this.data.typeid = this.data.folderList[index + 1].id;
                this.data.typename = this.data.folderList[index + 1].collect;
                let hasChildItem = this.data.folderList.filter(item => item.child && !item.id && item.collect != data.collect)[0];
                if (hasChildItem) {
                    let delIndex = this.data.folderList.findIndex(item => !item.id && item.collect == hasChildItem.collect);
                    if (delIndex >= 0) {
                        this.data.folderList[delIndex].child = false;
                        this.data.folderList[delIndex].childlength = 0;
                    }

                }
            }
        }

        ks.setNavigationBarTitle({
            title: this.data.typename ? this.data.typename : "请选择收藏夹名称",

        });

        this.setData({
            folderList: this.data.folderList,
            typeid: this.data.typeid,
            typename: this.data.typename,
        });

    },

    getChild(type) {
        return list.filter(item => item.type == type);
    },

    confirm() {
        if (this.data.typeid) {
            if (type == "new")
                util.request("user.php", {
                    action: "collect",
                    option: "collect_add",
                    user_id: ks.getStorageSync("user_id"),
                    openid: ks.getStorageSync("openid"),
                    type_id: this.data.typeid,
                }, "POST").then(res => {
                    if (valueid) {
                        if (apitype == "add") {
                            this.addCollect(valueid, this.data.typeid);
                        } else {
                            tools.moveCollect(this.data.typeid, valueid).then(res => {
                                ks.navigateBack({
                                    url: '/pages/mine/collect/index/index'
                                });
                            })
                        }
                    } else {
                        ks.showToast({
                            title: `创建成功`
                        });
                        ks.navigateBack();
                    }
                });
            else if (type == "rename")
                util.request("user.php", {
                    action: "collect",
                    option: "mod",
                    user_id: ks.getStorageSync("user_id"),
                    openid: ks.getStorageSync("openid"),
                    new_id: this.data.typeid,
                    type_id
                }, "POST").then(res => {
                    ks.showToast({
                        title: '收藏成功',
                    });
                    ks.navigateBack({
                        delta: 1
                    });
                })
        } else
            ks.showToast({
                title: '请选择收藏夹',
                icon: 'none',
            });
    },

    addCollect(id, type_id) {
        let data = {
            action: "collect",
            option: "add",
            valueid: id,
            user_id: ks.getStorageSync("user_id"),
            type_id,
            openid: ks.getStorageSync("openid"),
            path: "article",
            value,
        };
        util.request('user.php', data, 'POST').then(res => {
            if (res.data.state == 1) {
                ks.showToast({
                    title: res.data.message,
                });
                ks.navigateBack({});
            }

        });
    },

});